/*
 * JPanelTextField.java
 *
 * Created on 11 de octubre de 2008, 22:10
 */

package es.atareao.alejandria.gui;

import java.awt.Dimension;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author  Propietario
 */
public class JPanelFormattedField extends javax.swing.JPanel {

    /** Creates new form JPanelTextField */
    public JPanelFormattedField() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jFormattedTextField1 = new javax.swing.JFormattedTextField();

        setMaximumSize(new java.awt.Dimension(32767, 30));
        setMinimumSize(new java.awt.Dimension(0, 30));
        setPreferredSize(new java.awt.Dimension(50, 30));
        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.X_AXIS));

        jLabel1.setText("jLabel1");
        jLabel1.setMaximumSize(new java.awt.Dimension(2147483647, 25));
        jLabel1.setMinimumSize(new java.awt.Dimension(0, 25));
        jLabel1.setPreferredSize(new java.awt.Dimension(10, 25));
        add(jLabel1);

        jPanel1.setMaximumSize(new java.awt.Dimension(10, 25));
        jPanel1.setMinimumSize(new java.awt.Dimension(10, 25));
        jPanel1.setPreferredSize(new java.awt.Dimension(10, 25));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 25, Short.MAX_VALUE)
        );

        add(jPanel1);

        jFormattedTextField1.setMaximumSize(new java.awt.Dimension(2147483647, 25));
        jFormattedTextField1.setMinimumSize(new java.awt.Dimension(0, 25));
        jFormattedTextField1.setPreferredSize(new java.awt.Dimension(60, 25));
        add(jFormattedTextField1);
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
    private String _label;
    private String _value;
    private String _mask;
    private int _labelSize;

    public String getLabel() {
        this._label=this.jLabel1.getText();
        return _label;
    }

    public void setLabel(String label) {
        this._label = label;
        this.jLabel1.setText(label);
    }

    public String getValue() {
        this._value=this.jFormattedTextField1.getText();
        return _value;
    }

    public void setValue(String value) {
        this._value = value;
        this.jFormattedTextField1.setText(value);
    }

    public int getLabelSize() {
        this._labelSize=this.jLabel1.getWidth();
        return _labelSize;
    }

    public void setLabelSize(int labelSize) {
        this._labelSize = labelSize;
        this.jLabel1.setMaximumSize(new Dimension(labelSize,25));
        this.jLabel1.setMinimumSize(new Dimension(labelSize,25));
        this.jLabel1.setPreferredSize(new Dimension(labelSize,25));
    }

    public String getMask() {
        return _mask;
    }
    
    /**
     * Sets the mask for the formatted field
     * Character 	Description
     * #	Any valid number, uses Character.isDigit.
     * '	Escape character, used to escape any of the special formatting characters.
     * U	Any character (Character.isLetter). All lowercase letters are mapped to upper case.
     * L	Any character (Character.isLetter). All upper case letters are mapped to lower case.
     * A	Any character or number (Character.isLetter or Character.isDigit)
     * ?	Any character (Character.isLetter).
     * *	Anything.
     * H	Any hex character (0-9, a-f or A-F).
     * @param mask this is the mask
     */
    public void setMask(String mask) {
        try {
            this._mask = mask;
            this.jFormattedTextField1.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter(mask)));
        } catch (ParseException ex) {
            ErrorDialog.manejaError(ex);
        }
    }
}
